package com.test2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import java.util.HashMap;
import java.util.Map;

public class fxUtil {

    public static void main(String[] args) {
        String path = "d:\\geckodriver.exe";
        if(args.length > 0){
            path = args[0];
        }
        get("https://mall.q-gp.com/share/product?skuNo=366936136878081",path);
    }

    public static Map<String, String> get(String url,String path) {
        // 参数配置
        System.setProperty("webdriver.firefox.driver", path);

        FirefoxOptions option = new FirefoxOptions();
//        option.addArguments("headless"); // 无界面参数
//        option.addArguments("no-sandbox"); // 禁用沙盒
        // 通过ChromeOptions的setExperimentalOption方法，传下面两个参数来禁止掉谷歌受自动化控制的信息栏
        //option.setExperimentalOptions("useAutomationExtension", false);
        //option.setExperimentalOptions("excludeSwitches", Collections.singletonList("enable-automation"));


        //设置为 headless 模式 （必须）
        //option.addArguments("--headless");
        option.addArguments("--disable-gpu");//谷歌文档提到需要加上这个属性来规避bug
        option.addArguments("--no-sandbox");// “–no - sandbox”参数是让Chrome在root权限下跑
        option.addArguments("--disable-dev-shm-usage");
        option.addArguments("lang=zh_CN.UTF-8");
        //option.addArguments("window-size=1920x1080"); //指定浏览器分辨率

        WebDriver chromeDriver = new FirefoxDriver(option);

        String title = "";
        String num = "";
        String img = "";

        // 创建EventFiringWebDriver 实例
        EventFiringWebDriver driver= new EventFiringWebDriver(chromeDriver);
        // Creating instance of eventListener, that we just defined
        EventListenerType2 eventListener1=new EventListenerType2();
        // Register the Listener with the event firing driver
        driver.register(eventListener1);
        driver.get(url);

        long time = System.currentTimeMillis();
        while (img == null ) {
            img = driver.findElement(By.className("cr-image--img")).getAttribute("src");
            title = driver.findElement(By.className("title")).getText();
            num = driver.findElement(By.className("num")).getText();
            if (System.currentTimeMillis() - time > 5000) {
                System.out.println("获取超时，已结束");
                //driver.quit();
                return null;
            }
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("标题:" + title);
        System.out.println("价格:" + num);
        System.out.println("图片:" + img);
        //driver.quit();
        Map<String, String> map = new HashMap();
        map.put("title", title);
        map.put("num", num);
        map.put("img", img);
        return map;
    }
}

